package com.wjh.rules.core.factory;

import com.wjh.rules.assembler.ExpressAssembler;
import com.wjh.rules.common.ResponseMessage;
import com.wjh.rules.common.RuleEntity;
import com.wjh.rules.core.rule.RuleUnit;

/**
 * 规则工厂
 *
 * @author wjh
 */
public class RuleUnitFactory {

    /**
     * 规则组装器
     */
    public static ExpressAssembler ea = ExpressAssembler.getInstance();

    public static ResponseMessage<RuleUnit> create(RuleEntity re) {
        RuleUnit ru = new RuleUnit();
        ru.setId(re.getId());
        ru.setGroupId(re.getGroupId());
        String express = re.getExpress();
        String ruleInfo = re.getRuleInfo();
        ru.setRuleInfo(ruleInfo);
        ResponseMessage<String> rm = ea.assemble(express);
        if (rm.getCode() == ResponseMessage.FAIL) {
            return ResponseMessage.fail(rm.getMsg());
        }
        ru.setExpress(rm.getData());
        return ResponseMessage.succ(ru);
    }
}
